package com.hanthink.web.controller.app.equipment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.equipment.EquipmentInfo;
import com.hanthink.system.domain.equipment.UserAssigned;
import com.hanthink.system.mapper.equipment.EquipmentInfoMapper;
import com.hanthink.system.service.equipment.IEquipmentInfoService;

@RestController
@RequestMapping("/app/equipment")
public class EquipmentInfoControll extends BaseController {

	@Autowired
	private IEquipmentInfoService equipmentInfoService;

	@Autowired
	private EquipmentInfoMapper equipmentInfoMapper;

	@Autowired
	private TokenService tokenService;

	@PostMapping("/info")
	public TableDataInfo list(@RequestBody EquipmentInfo info) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L) {
			info.setSiteKey(loginUser.getUser().getSite());
		}
		EquipmentInfo equipmentInfo = equipmentInfoService.selectEquipmentInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		List<EquipmentInfo> infos = new ArrayList<EquipmentInfo>();
		infos.add(equipmentInfo);
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("操作成功");
		tableDataInfo.setRows(infos);
		return tableDataInfo;
	}

	/**
	 * 查询当前夜班指派人员
	 * 
	 * @param map
	 * @return
	 */
	@PostMapping("/userAssignedlist")
	public TableDataInfo userAssignedlist(@RequestBody Map<String, Object> map) {
		startPage();
		List<UserAssigned> list = equipmentInfoService.userAssignedlist(map);
		return getDataTable(list);
	}

	/**
	 * 人员指派保存
	 * 
	 * @param map
	 * @return
	 */
	@PostMapping("/save")
	public TableDataInfo save(@RequestBody Map<String, Object> map) {
		Object id = map.get("id"); // ID
		Object userName = map.get("userName");
		Object startTime = map.get("startTime");
		Object endTime = map.get("endTime");
		Object status = map.get("status");
		Object siteKey = map.get("siteKey");

		if (id == null) {
			// 新增
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("siteKey", Long.valueOf(siteKey.toString()));
			params.put("userName", userName.toString());
			params.put("startTime", startTime.toString());
			params.put("endTime", endTime.toString());
			params.put("status", status.toString());
			equipmentInfoMapper.insertInfo(params);
		} else {
			// 更新维修记录状态
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("id", id.toString());
			params.put("userName", userName.toString());
			params.put("startTime", startTime.toString());
			params.put("endTime", endTime.toString());
			params.put("status", status.toString());
			equipmentInfoMapper.save(params);
		}

		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(HttpStatus.SUCCESS);
		rspData.setMsg("操作成功");
		return rspData;
	}

}
